﻿using System.Windows;
using System.Windows.Controls;

namespace Notoric.Gui.Windows
{
    /// <summary>
    /// Dialog for editing the appearance of a <see cref="ViewModel.Elements.Page"/>.
    /// </summary>
    public partial class PageAppearanceDialog : Window
    {
        #region Constructors

        /// <summary>
        /// Creates a new <see cref="PageAppearanceDialog"/> instance.
        /// </summary>
        public PageAppearanceDialog()
        {
            InitializeComponent();
        }

        #endregion

        #region Dependency Property Fields

        /// <summary>
        /// Identifies the <see cref="Page"/> dependency property.
        /// </summary>
        public static readonly DependencyProperty PageProperty = DependencyProperty.Register(
            "Page", typeof(ViewModel.Elements.Page), typeof(PageAppearanceDialog)
            );

        #endregion

        #region Event Handlers

        /// <summary>
        /// Occurs when the user clicks the OK button.
        /// </summary>
        /// <param name="sender">Object where the event handler is attached</param>
        /// <param name="e">Event data</param>
        private void PART_OK_Click(object sender, RoutedEventArgs e)
        {
            Page.CommitChanges();
            this.DialogResult = true;
        }

        /// <summary>
        /// Occurs when the user clicks the Cancel button.
        /// </summary>
        /// <param name="sender">Object where the event handler is attached</param>
        /// <param name="e">Event data</param>
        private void PART_Cancel_Click(object sender, RoutedEventArgs e)
        {
            Page.AbortChanges();
            this.DialogResult = false;
        }

        /// <summary>
        /// Occurs when the portrait orientation toggle button is checked.
        /// </summary>
        /// <param name="sender">Object where the event handler is attached</param>
        /// <param name="e">Event data</param>
        private void PART_Portrait_Checked(object sender, RoutedEventArgs e)
        {
            PART_Landscape.IsChecked = false;
            Page.Orientation = ViewModel.Elements.PageOrientation.Portrait;
        }

        /// <summary>
        /// Occurs when the landscape orientation toggle button is checked.
        /// </summary>
        /// <param name="sender">Object where the event handler is attached</param>
        /// <param name="e">Event data</param>
        private void PART_Landscape_Checked(object sender, RoutedEventArgs e)
        {
            PART_Portrait.IsChecked = false;
            Page.Orientation = ViewModel.Elements.PageOrientation.Landscape;
        }

        /// <summary>
        /// Occurs when the window has been loaded.
        /// </summary>
        /// <param name="sender">Object where the event handler is attached</param>
        /// <param name="e">Event data</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (Page.Orientation == ViewModel.Elements.PageOrientation.Landscape)
                PART_Landscape.IsChecked = true;
            else
                PART_Portrait.IsChecked = true;

            PART_TopMargin.Value = Page.Margin.Top;
            PART_TopMargin.ValueChanged += new RoutedPropertyChangedEventHandler<double>(PART_Margin_ValueChanged);
            PART_BottomMargin.Value = Page.Margin.Bottom;
            PART_BottomMargin.ValueChanged += new RoutedPropertyChangedEventHandler<double>(PART_Margin_ValueChanged);
            PART_LeftMargin.Value = Page.Margin.Left;
            PART_LeftMargin.ValueChanged += new RoutedPropertyChangedEventHandler<double>(PART_Margin_ValueChanged);
            PART_RightMargin.Value = Page.Margin.Right;
            PART_RightMargin.ValueChanged += new RoutedPropertyChangedEventHandler<double>(PART_Margin_ValueChanged);
        }

        /// <summary>
        /// Occurs when the value of a margin spinner has changed.
        /// </summary>
        /// <param name="sender">Object where the event handler is attached</param>
        /// <param name="e">Event data</param>
        private void PART_Margin_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            Page.Margin = new Thickness(PART_LeftMargin.Value, PART_TopMargin.Value, PART_RightMargin.Value, PART_BottomMargin.Value);
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the page edited by this dialog.
        /// </summary>
        /// <value>
        /// <para>
        /// Type: <see cref="ViewModel.Elements.Page"/>
        /// </para>
        /// <para>
        /// The page edited by this dialog.
        /// </para>
        /// </value>
        public ViewModel.Elements.Page Page
        {
            get { return (ViewModel.Elements.Page)GetValue(PageProperty); }
            set { SetValue(PageProperty, value); }
        }

        #endregion
    }
}
